/* ----------------------------------------------------------------------
*
* Copyright (C) 2025 XiaoFei. All rights reserved.
*
* @file:		YeHuo_DS18B20.c
* @author:		XiaoFei
* @Date:     	18-March-2025
* @Revision: 	V1
* @Project: 	XiaoFei BSP Library
*
* -------------------------------------------------------------------- */

#include "stm32f10x.h"
#include "YeHuo_DS18B20.h"
#include "XiaoFei_Delay.h"
#include "stdlib.h"

/* 
* @function: 	DS18B20_Check
* @return:		Return 0x01 if the device is connect.
*/
static unsigned char DS18B20_Check(void)
{
	unsigned char time=0x00;
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin=DS18B20_DQ_Pin;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;	
	GPIO_Init(DS18B20_DQ_GPIO,&GPIO_InitStructure);
	while(DS18B20_ReadDQ&&time<100)
	{
		time++;
		XiaoFei_Delay_us(1);
	}	
	if(time>=100)
		return 1;
	else
		time = 0;
	while(!DS18B20_ReadDQ&&time<240)
	{
		time++;
		XiaoFei_Delay_us(1);
	}	
	return time>=240;
}

/* 
* @function: 	DS18B20_Init
* @parameter:	[in] DS18B20				The Device handle of DS18B20.
* @return:		Return 0x01 if the device is connect.
*/
unsigned char DS18B20_Init(DS18B20_TypeDef* DS18B20)
{
	DS18B20->ID=(unsigned char*)malloc(8*sizeof(unsigned char));
	DS18B20->DataH=0x00;
	DS18B20->DataL=0x00;
	DS18B20->Temp=0;
	GPIO_InitTypeDef GPIO_InitStructure;
	DS18B20_RCC_DQ(1); 													   
	GPIO_InitStructure.GPIO_Pin=DS18B20_DQ_Pin;	
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;   
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; 
	GPIO_Init(DS18B20_DQ_GPIO,&GPIO_InitStructure);
	DS18B20_DQ(1);
	DS18B20_DQ(0);
	XiaoFei_Delay_us(750);
	DS18B20_DQ(1);
	XiaoFei_Delay_us(15);
	return DS18B20_Check();
}

/* 
* @function: 	DS18B20_ReadByte
* @return:		The data from DS18B20.
*/
static unsigned char DS18B20_ReadByte(void)
{
	unsigned char j,dat=0x00;	
	for(unsigned char i=0;i<8;i++) 
	{
		GPIO_InitTypeDef GPIO_InitStructure;														   
		GPIO_InitStructure.GPIO_Pin=DS18B20_DQ_Pin;	
		GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;   
		GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
		GPIO_Init(DS18B20_DQ_GPIO,&GPIO_InitStructure);
		DS18B20_DQ(0);
		XiaoFei_Delay_us(10);
		GPIO_InitStructure.GPIO_Pin=DS18B20_DQ_Pin;
		GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;	
		GPIO_Init(DS18B20_DQ_GPIO,&GPIO_InitStructure);
		j=DS18B20_ReadDQ;
		XiaoFei_Delay_us(45);
		dat=(dat)|(j<<i);
	}
	return dat;
}

/* 
* @function: 	DS18B20_WriteByte
* @parameter:	[in] dat				    The data will be write to DS18B20.
*/
static void DS18B20_WriteByte(unsigned char dat)
{
	GPIO_InitTypeDef GPIO_InitStructure;													   
	GPIO_InitStructure.GPIO_Pin=DS18B20_DQ_Pin;	
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;   
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(DS18B20_DQ_GPIO,&GPIO_InitStructure);
	for(unsigned char i=0;i<8;i++)
	{
		unsigned char testb=dat&0x01;
		dat = dat>>1;		
		DS18B20_DQ(0);
		XiaoFei_Delay_us(testb?8:70);
		DS18B20_DQ(1);
		XiaoFei_Delay_us(testb?58:2);
	}
}

/* 
* @function: 	DS18B20_MatchRom
*/
static void DS18B20_MatchRom(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;												   
	GPIO_InitStructure.GPIO_Pin=DS18B20_DQ_Pin;	
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;   
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(DS18B20_DQ_GPIO,&GPIO_InitStructure);
	DS18B20_DQ(0);
	XiaoFei_Delay_us(750);
	DS18B20_DQ(1);
	XiaoFei_Delay_us(15);   
	DS18B20_Check();	 
	DS18B20_WriteByte(0X55);	
}

/* 
* @function: 	DS18B20_Ready
* @parameter:	[in] DS18B20				The Device handle of DS18B20.
*/
void DS18B20_Ready(DS18B20_TypeDef* DS18B20)
{
	DS18B20_WriteByte(0x33);
	for (unsigned char i=0;i<8;i++)
        DS18B20->ID[i]=DS18B20_ReadByte();
}

/* 
* @function: 	DS18B20_GetData
* @parameter:	[out] DS18B20				The Device handle of DS18B20.
*/
void DS18B20_GetData(DS18B20_TypeDef* DS18B20)
{
	DS18B20_MatchRom();
    for(unsigned char i=0;i<8;i++)
        DS18B20_WriteByte(DS18B20->ID[i]);	
	DS18B20_WriteByte(0X44);
	DS18B20_MatchRom(); 
	for(unsigned char i=0;i<8;i++)
		DS18B20_WriteByte(DS18B20->ID[i]);	
	DS18B20_WriteByte(0XBE);
	DS18B20->DataL=DS18B20_ReadByte();		 
	DS18B20->DataH=DS18B20_ReadByte(); 
	short int s_tem=(DS18B20->DataH<<8)|DS18B20->DataL;
	if(s_tem<0)
		DS18B20->Temp=(~s_tem+1)*0.0625;
	else
		DS18B20->Temp=s_tem*0.0625;
}

